﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;

namespace CaseManagerLibrary.Log
{
    /// <summary>
    /// 
    /// </summary>
    public class ConsoleLog : ILog
    {
        public bool BringToFront { get; set; }
        public int Left { get; set; }

        public int Top { get; set; }

        public bool Writing { get; private set; }

        private const string blank = "                         ";

        public ConsoleLog()
        {
            BringToFront = true;
            Left = 0;
            Top = 0;
            Writing = false;
        }

        public void Close()
        {
        }

        public void Info(string text)
        {
            try
            {
                if (BringToFront)
                {
                    Console.SetCursorPosition(this.Left, this.Top);
                }
                Console.WriteLine("");
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.Write(this.Left + "," + this.Top + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("  INFO:" + text);
                Console.ForegroundColor = ConsoleColor.White;
                this.Left = Console.CursorLeft;
                this.Top = Console.CursorTop;
            }
            catch (Exception ex)
            { }
        }

        public void Error(string text)
        {
            try
            {
                if (BringToFront)
                {
                    Console.SetCursorPosition(this.Left, this.Top);
                }
                Console.WriteLine("");
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.Write(this.Left + "," + this.Top + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("  ERROR:" + text);
                Console.ForegroundColor = ConsoleColor.White;
                this.Left = Console.CursorLeft;
                this.Top = Console.CursorTop;
            }
            catch (Exception ex)
            { }
        }

        public void Log(string text)
        {
            try
            {
                if (this.BringToFront && (this.Left != Console.CursorLeft || this.Top != Console.CursorTop))
                {
                    Console.SetCursorPosition(this.Left, this.Top);
                }
                Console.WriteLine("");
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.Write(this.Left + "," + this.Top + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("  LOG:" + text);

                if (this.BringToFront)
                {
                    this.Left = Console.CursorLeft;
                    this.Top = Console.CursorTop;
                }
            }
            catch (Exception ex)
            { }
        }

        public void Text(string text)
        {
            try
            {
                if (BringToFront)
                {
                    Console.SetCursorPosition(this.Left, this.Top);
                }
                Console.WriteLine("");
                Console.Write(this.Left + "," + this.Top + blank + text);
                this.Left = Console.CursorLeft;
                this.Top = Console.CursorTop;
            }
            catch (Exception ex)
            { }
        }

        public void NewLine()
        {
            try
            {
                if (BringToFront)
                {
                    Console.SetCursorPosition(this.Left, this.Top);
                }
                Console.WriteLine("");
                Console.Write(this.Left + "," + this.Top);
                this.Left = Console.CursorLeft;
                this.Top = Console.CursorTop;
            }
            catch (Exception ex)
            { }
        }

        public void MC()
        {
            try
            {
                Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop);
                Console.Write("MC>");
            }
            catch (Exception ex)
            { }
        }
    }
}
